package zhj.test.ballmove;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BounceFrame extends JFrame {


	private BallPanel panel;
	public static final int DEFAULT_WIDTH = 450;
	public static final int DEFAULT_HEIGHT = 350;
	public static final int STEPS = 1000;



	public BounceFrame()
	{

		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		setTitle("Bounce");

		panel = new BallPanel();
		add(panel, BorderLayout.CENTER);

		JPanel buttonPanel = new JPanel();

		addButton(buttonPanel, "Start",
				new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				addBall();
			}
		});

		addButton(buttonPanel, "Close",
				new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				System.exit(0);
			}
		});


		add(buttonPanel, BorderLayout.SOUTH);

	}

	public void addButton(Container c, String title, ActionListener listener)
	{
		JButton button = new JButton(title);
		c.add(button);
		button.addActionListener(listener);
	}

	public void addBall()
	{
		for(int i=0;i<1000;i++)
		{
			Ball ball = new Ball();
			ball.setX(i);
			ball.setY(i);
			panel.addBall(ball);
		}
		

		Thread1 thread = new Thread1(this, panel);
		thread.start();

	}


}
